/*
 * Bomberman.cpp
 *
 *  Created on: 21/05/2014
 *      Author: lucio
 */

#include "Bomberman.h"
#include "Utils.h"
//Screen attributes
const int SCREEN_BPP = 32;

//The frame rate
const int FRAMES_PER_SECOND = 20;

//The dot dimensions
const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;

//The dimensions of the level
const int LEVEL_WIDTH = 1280;
const int LEVEL_HEIGHT = 960;

void Bomberman::handle_input() {
    if( event.type == SDL_KEYDOWN ) {
        switch((int)event.key.keysym.sym)
        {
            case SDLK_UP: yVel -= DOT_HEIGHT / 2; break;
            case SDLK_DOWN: yVel += DOT_HEIGHT / 2; break;
            case SDLK_LEFT: xVel -= DOT_WIDTH / 2; break;
            case SDLK_RIGHT: xVel += DOT_WIDTH / 2; break;
        }
    } else if(event.type == SDL_KEYUP) {
        switch((int)event.key.keysym.sym) {
            case SDLK_UP: yVel += DOT_HEIGHT / 2; break;
            case SDLK_DOWN: yVel -= DOT_HEIGHT / 2; break;
            case SDLK_LEFT: xVel += DOT_WIDTH / 2; break;
            case SDLK_RIGHT: xVel -= DOT_WIDTH / 2; break;
        }
    }
}

void Bomberman::move(){
    x += xVel;

    if((x < 0) || (x + DOT_WIDTH > LEVEL_WIDTH)) {
        x -= xVel;
    }

    y += yVel;

    if((y < 0) || (y + DOT_HEIGHT > LEVEL_HEIGHT)) {
        y -= yVel;
    }
}

void Bomberman::show(){
	Utils::apply_surface(x - this->cam.x, y - this->cam.y, this->object, this->screen, &this->cam);
}

void Bomberman::set_camera(){
    this->cam.x = (x + DOT_WIDTH / 2) - SCREEN_WIDTH / 2;
    this->cam.y = (y + DOT_HEIGHT / 2) - SCREEN_HEIGHT / 2;

    if(this->cam.x < 0) {
        this->cam.x = 0;
    }
    if(this->cam.y < 0) {
        this->cam.y = 0;
    }
    if(this->cam.x > LEVEL_WIDTH - this->cam.w) {
        this->cam.x = LEVEL_WIDTH - this->cam.w;
    }
    if(this->cam.y > LEVEL_HEIGHT - this->cam.h) {
        this->cam.y = LEVEL_HEIGHT - this->cam.h;
    }
}

Bomberman::Bomberman(SDL_Surface* screen, const SDL_Rect& rect) {
    this->x = 0;
    this->y = 0;
    this->xVel = 0;
    this->yVel = 0;
    this->object = NULL;
    this->screen = screen;
    this->cam = rect;
}

Bomberman::~Bomberman() {
	// TODO Auto-generated destructor stub
}

